<template>
  <div class="top-category">
    <div class="container">

      <!-- 全部分类 -->
      <div class="sub-list">
        <h3>全部分类</h3>
        <ul>
          <li v-for="cate in cateList" :key="cate.id">
            <a href="javascript:;">
              <img :src="cate.picture" />
              <p>{{ cate.name }}</p>
            </a>
          </li>
        </ul>
      </div>

      <!-- end -->
      <!-- 分类关联商品 -->
      <div class="ref-goods" v-for="item in cateList" :key="item.id">
        <div class="head">
          <!-- 子分类名字 -->
          <h3>-  {{ item.name }}  -</h3>
          <XtxMore to="/" />
        </div>
        <div class="body">
          <!-- 子分类关联商品列表 -->
          <RouterLink to="/" class="goods-item" v-for="good in item.goods" :key="good.id">
            <img :src="good.picyure" alt="" />
            <p class="name ellipsis">{{ good.name }}</p>
            <p class="desc ellipsis">{{ good.desc }}</p>
            <p class="price">&yen;{{ good.price }}</p>
          </RouterLink>
        </div>
      </div>

    </div>
  </div>
</template>

<script>
import { findAllCategoryApi } from '@/api/category'
import { onMounted, ref, watch } from 'vue'
import { onBeforeRouteUpdate, useRoute } from 'vue-router'

export default {
  name: 'TopCategory',
  setup () {
    // 2. 存储分类数据
    const cateList = ref([])
    // 使用 useRoute 钩子函数获取 $route 对象
    // route === 等价于 this.$route
    const route = useRoute()
    // 1. 获取分类数据
    const getCateList = async (id) => {
      const { data } = await findAllCategoryApi(id)
      // console.log('分类数据: ', data)
      cateList.value = data.children
    }
    onMounted(() => {
      getCateList(route.params.id)
    })

    // 解决路由缓存问题: 路径不变
    // 方案1: 使用 watch 函数监听 route.params.id 参数变化
    // watch(() => route.params.id, (newVal, oldVal) => {
    //   console.log('页面切换: ', newVal, oldVal)
    //   getCateList(newVal)
    // })
    // 方案2: 使用路由 onBeforeRouteUpdate 钩子函数获取最新 ID 参数刷新数据
    // onBeforeRouteUpdate((to) => {
    //   // console.log('页面切换: ', to)
    //   getCateList(to.params.id)
    // })
    // 方案3: 在子组件挂载点, 给 router-view 加上 key 值为每次路由切换的完整路径

    return { cateList }
  }
  // created () {
  //   console.log('vue2获取动态路由的参数: ', this.$route.params.id)
  // }
}
</script>

<style scoped lang="less">
.top-category {
  h3 {
    font-size: 28px;
    color: #666;
    font-weight: normal;
    text-align: center;
    line-height: 100px;
  }
  .sub-list {
    margin-top: 20px;
    background-color: #fff;
    ul {
      display: flex;
      padding: 0 32px;
      flex-wrap: wrap;
      li {
        width: 168px;
        height: 160px;
        a {
          text-align: center;
          display: block;
          font-size: 16px;
          img {
            width: 100px;
            height: 100px;
          }
          p {
            line-height: 40px;
          }
          &:hover {
            color: @xtxColor;
          }
        }
      }
    }
  }
  .ref-goods {
    background-color: #fff;
    margin-top: 20px;
    position: relative;
    .head {
      .xtx-more {
        position: absolute;
        top: 20px;
        right: 20px;
      }
      .tag {
        text-align: center;
        color: #999;
        font-size: 20px;
        position: relative;
        top: -20px;
      }
    }
    .body {
      display: flex;
      justify-content: space-around;
      padding: 0 40px 30px;
    }
    .goods-item {
      display: block;
      width: 220px;
      padding: 20px 30px;
      text-align: center;
      img {
        width: 160px;
        height: 160px;
      }
      p {
        padding-top: 10px;
      }
      .name {
        font-size: 16px;
      }
      .desc {
        color: #999;
        height: 29px;
      }
      .price {
        color: @priceColor;
        font-size: 20px;
      }
    }
  }
}
</style>
